package cn.wolfcode.p2p.bussness.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.query.QueryObject;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.bussness.domain.PlatformBankInfo;
import cn.wolfcode.p2p.bussness.domain.RechargeOffline;
import cn.wolfcode.p2p.bussness.mapper.RechargeOfflineMapper;
import cn.wolfcode.p2p.bussness.service.IAccountFlowService;
import cn.wolfcode.p2p.bussness.service.IPlatformBankInfoService;
import cn.wolfcode.p2p.bussness.service.IRechargOfflineService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RechargOfflineServiceImpl implements IRechargOfflineService {

    @Autowired //线下充值
    private RechargeOfflineMapper rechargeOfflineMapper;

    @Autowired //平台的账户信息
    private IPlatformBankInfoService platformBankInfoService;

    @Autowired //账单流水信息
    private IAccountFlowService accountFlowService;

    @Autowired //用户账户信息
    private IAccountService accountService;

    @Override
    public int save(RechargeOffline rechargeOffline) {
        return rechargeOfflineMapper.insert(rechargeOffline);
    }

    @Override
    public int update(RechargeOffline rechargeOffline) {
        return rechargeOfflineMapper.updateByPrimaryKey(rechargeOffline);
    }

    @Override
    public RechargeOffline get(Long id) {
        return rechargeOfflineMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryPage(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<RechargeOffline> list = rechargeOfflineMapper.selectList(qo);
        return new PageInfo(list);
    }

    /**
     * 线下充值
     * @param rechargeOffline 充值订单
     */
    @Override
    public void apply(RechargeOffline rechargeOffline) {
        RechargeOffline ra = new RechargeOffline();
        //设置申请人 线下充值 当前登陆的用户 申请时间
        ra.setApplier(UserContext.getCurrent());
        ra.setApplyTime(new Date());
        //设置充值金额
        ra.setAmount(rechargeOffline.getAmount());
        //设置平台账户信息
        PlatformBankInfo bankInfo = platformBankInfoService.get(rechargeOffline.getBankInfo().getId());
        ra.setBankInfo(bankInfo);
        //设置交易号
        ra.setTradeCode(rechargeOffline.getTradeCode());
        //设置充值时间
        ra.setRechargeDate(rechargeOffline.getRechargeDate());
        //设置备注信息
        ra.setNote(rechargeOffline.getNote());
        //设置待审核状态
        ra.setState(RechargeOffline.STATE_NORMAL);
        //保存到数据库中
        this.save(ra);
    }

    /**
     *
     * @param id    充值单对象ID
     * @param state 审核状态
     * @param remark 审核内容
     */
    @Override
    public void audit(Long id, Integer state, String remark) {
        RechargeOffline rechargeOffline = this.get(id);
        //rechargeOffline不为null 并且申请状态是待审核状态
        if(rechargeOffline != null && rechargeOffline.getState() == RechargeOffline.STATE_NORMAL){
            //设置审核时间  审核人
            rechargeOffline.setAuditTime(new Date());
            rechargeOffline.setAuditor(UserContext.getCurrent());
            //设置审核内容
            rechargeOffline.setRemark(remark);

            //拿到当前用户的账户信息
            Account userAccount = accountService.get(rechargeOffline.getApplier().getId());
            //如果审核通过
            if(state == RechargeOffline.STATE_PASS){
                //设置状态
                rechargeOffline.setState(RechargeOffline.STATE_PASS);
                //申请人的可用金额增加
                //充值前的可用金额 + 充值进来的金额
                userAccount.setUsableAmount(userAccount.getUsableAmount().add(rechargeOffline.getAmount()));
                //更新到数据库中
                accountService.update(userAccount);
                //生成充值成功的流水 并且状态是 可用余额增加
                accountFlowService.createRechargeOfflineFlow(userAccount,rechargeOffline.getAmount(),remark,
                        Constants.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE);
            }else {
                //审核不通过
                //设置状态
                rechargeOffline.setState(RechargeOffline.STATE_REJECT);
                //生成充值失败的流水 充值后余额为0
                accountFlowService.createRechargeOfflineFlow(userAccount,new BigDecimal(0.00),
                        remark,Constants.ACCOUNT_ACTIONTYPE_RECHARGE_REJECT);
            }
            //更新到数据库中
            this.update(rechargeOffline);
        }
    }
}
